package com.lihonghao.gateway.bean;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.ClassUtil;
import com.lihonghao.gateway.DefaultGate;
import com.lihonghao.gateway.Router;

import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ModelContainer {
    //存储对象名称和已经实例化的对象映射
    private static ConcurrentHashMap<String,Object> beanMap = new ConcurrentHashMap<>();
    //存储对象名称和对应对象信息的映射
    private static ConcurrentHashMap<String,BeanDefinition> beanDefineMap= new ConcurrentHashMap<>();
    //存储存储在容器中对象的名称
    private static Set<String> beanNameSet = Collections.synchronizedSet(new HashSet<>());


    public ModelContainer(Set<Class<?>> classes) {
        for (Class<?> clazz : classes) {
            try {
                String className = ClassUtil.getClassName(clazz,true);
                Object bean =  clazz.getConstructor().newInstance();
                beanNameSet.add(className);
                beanMap.put(className, bean);
                Method[] methods = clazz.getDeclaredMethods();
                beanDefineMap.put(className, new BeanDefinition(clazz,className,bean,methods));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public <T> T getBean(String beanName) {
        if (beanNameSet.contains(beanName)) {
            return (T) beanMap.get(beanName);
        }
        return null;
    }

//    private class Converter{
//        <T> T convert(Object source, Class<T> target) {
//
//        }
//    }
}
